package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.phonehelp.JinDunRecord;
import com.ucredit.babel.common.model.third.phonehelp.PhoneHelpData;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 电话邦金盾号码卫士
 *
 * @author renzhanzheng
 */

public class JinDunParse extends ParseBase {
    private Text key = new Text();
    private Text val = new Text();

    /**
     * 电话邦金盾号码卫士
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     * @return
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        JinDunRecord obj = (JinDunRecord) model;
        PhoneHelpData data = obj.getData();
        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());
        JSONObject jsonData = info.getJSONObject("data") == null ? new JSONObject() : info.getJSONObject("data");
        StringBuilder sb = getBaseInfo(obj, successful);

        //主接口描述
        sb.append(info.getString("msg") == null ? "" : info.getString("msg")).append(CRT001);
        //接口状态码\含义见接口文档状态码描述
        sb.append(info.getString("status") == null ? "" : info.getString("status")).append(CRT001);
        //sid
        sb.append(info.getString("sid") == null ? "" : info.getString("sid")).append(CRT001);

        StringBuilder detailSb = new StringBuilder(500);
        StringBuilder txSb = getBaseInfo(obj);
        int i = 0;
        for (Object value : jsonData.values()) {
            i++;
            JSONObject v = (JSONObject) value;
            //序号
            detailSb.append(i).append(CRT001);
            //贷前外呼风险查询结果1\高风险\2\关注级风险\0\未查得
            detailSb.append(v.getString("loan")).append(CRT001);
            //当天查询次数查询结果0-100
            detailSb.append(v.getString("today_calls")).append(CRT001);
            //小号中介号查询结果1\存在0\未查得
            detailSb.append(v.getString("trumpet")).append(CRT001);
            //近10天查询次数查询结果0-100
            detailSb.append(v.getString("last_10_day_calls")).append(CRT001);
            //号码活跃指数查询结果0-1000
            detailSb.append(v.getString("active")).append(CRT001);
            //号码单位行业背景查询结果
            detailSb.append(v.getString("category")).append(CRT001);

            key.set(getKey());
            val.set((sb.toString() + detailSb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        if (i == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        return true;
    }


    @Override
    public String getTableName() {
        return "kafka_babel_phonehelp_jindun_inc";
    }

    @Override
    public String getType() {
        return RecordType.JINDUN_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "电话邦金盾号码卫士";
    }
}
